package mumu.android.lib.tool;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;

public class DeviceUuidFactory {
	protected static final String PREFS_FILE = "device_id.xml";
	protected static final String PREFS_DEVICE_ID = "device_id";
	protected static UUID uuid;

	public static String GetCombineDeviceID(Context mContext) {
		//if (Tools.isNetworkAvailable(mContext)) {
			String combineDeviceIdFormat = "deviceid=%sserial=%smac=%s";
			String deviceid = GetDeviceid(mContext);
			String buildSerial = GetBuildSERIALs();
			String macaddress = GetWifiMac(mContext);
			String combineId = String.format(combineDeviceIdFormat, deviceid, buildSerial, macaddress);
			MLog.d("dd", combineId);
			try {
				return EncryptTool.desSEncrypt(combineId, "asdfasdf");
			} catch (Exception e) {
				try {
					return UUID.nameUUIDFromBytes(combineId.getBytes("utf8")).toString();
				} catch (UnsupportedEncodingException e1) {
					e1.printStackTrace();
					return combineId;

				}
			}
		//}
		//return "";
	}

	public static String GetANDROIDID(Context mContext) {
		return Secure.getString(mContext.getContentResolver(), Secure.ANDROID_ID);
	}

	public static String GetDeviceid(Context mContext) {
		final String deviceId = ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		return deviceId == null ? "0" : deviceId;
	}

	public static String GetSerialNumber(Context mContext) {
		final String deviceId = ((TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE)).getSimSerialNumber();
		return deviceId;
	}

	public static String GetWifiMac(Context mContext) {
		WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
		String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
		return m_szWLANMAC == null ? "" : m_szWLANMAC;
	}

	public static String GetBuildSERIALs() {
		String serial = Build.SERIAL;
		return serial == null ? "" : serial;
	}

	public static String GetBuild() {
		String str = "BOARD=" + Build.BOARD + " BRAND=" + Build.BRAND + " CPU_ABI=" + Build.CPU_ABI + " DEVICE=" + Build.DEVICE + " DISPLAY" + Build.DISPLAY + " HOST="
				+ Build.HOST + " ID=" + Build.ID + " MANUFACTURER=" + Build.MANUFACTURER + " MODEL=" + Build.MODEL + " PRODUCT=" + Build.PRODUCT + " TAGS=" + Build.TAGS + " TYPE="
				+ Build.TYPE + " SERIAL=" + Build.SERIAL + " USER=" + Build.USER; // 13
																					// digits
		return str;
	}

	public DeviceUuidFactory(Context context) {
		if (uuid == null) {
			synchronized (DeviceUuidFactory.class) {
				if (uuid == null) {
					final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
					final String id = prefs.getString(PREFS_DEVICE_ID, null);
					if (id != null) {
						// Use the ids previously computed and stored in the
						// prefs file
						uuid = UUID.fromString(id);
					} else {
						final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
						// Use the Android ID unless it's broken, in which case
						// fallback on deviceId,
						// unless it's not available, then fallback on a random
						// number which we store
						// to a prefs file
						try {
							if (!"9774d56d682e549c".equals(androidId)) {
								uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
							} else {
								final String deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
								uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
							}
						} catch (UnsupportedEncodingException e) {
							throw new RuntimeException(e);
						}
						// Write the value out to the prefs file
						prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();
					}
				}
			}
		}
	}
}